<?php
class SendPlugin {
    var $ld;
    var $bot;
    var $rep;
	var $xmlsFName;
	var $xmlsOb;
    // ==========================================================================
    function SendPlugin() {
		$this->ld = new LocalData();
		$bot = new Bot();
		$this->xmlsOb = new xmlsOb();
		$xmlsFNameObj = new xmlsLang();
		$this->xmlsFName = $xmlsFNameObj->langXML;
	}

	function flN($item) {
		if (!isset($this->xmlsFName) || empty($this->xmlsFName)) {
			return '-';
		}
		if (isset($this->xmlsFName[$item]) && (!empty($this->xmlsFName[$item]))) {
			return ((string)$this->xmlsFName[$item]);
		} else {
			return '';
		}
	}
	
	function itemIcon($item) {
      if (isset($item->image)) {
      	foreach ($item->image as $image) {
      	  if (strcasecmp($image['name'], 'icon') == 0) {
      	  	$imgurl = ''.@$image['url'];
      	  	if (!empty($imgurl)) {
      	  	  //return getcwd().'/'.$imgurl;
                    return (substr($_SERVER["PHP_SELF"],0,strpos($_SERVER["PHP_SELF"],"/")).$imgurl);
      	  	}
      	  	else return false;
      	  }
      	}
      }
      return false;
    }

	function GetItems() {
		$rep=Array();
		$list = '';
		$wot = array('MechanicMapResource','MunicipalCenter','Sidewalk','Neighborhood','Airplane','CraftingCenter','HolidayTown','Ship','HarvestableShip','Decoration','Residence','Business','Municipal','Landmark','Wonder','Storage','Mall','Bridge','Hotel','PowerStation','ConstructionSite','Wilderness');
		$res=$this->ld->GetSelect("select * from objects where className IN ('".implode("', '", $wot)."') order by className");
		if (!$res) { return "<option>DB ERROR</option>"; }
		$is=1;
		foreach ($res as $val) {
			$v=(string)$val[1];
			$obj=unserialize(base64_decode($v));
			$rep["obj"][$i] = $obj;
			$i++;
		}
		foreach ($rep["obj"] as $value) {
			$fname = ' - '.$this->flN((string)$value['itemName']);
			$list .= '<option value="'.$value['id'].'">'.$value['className'].' - '.$value['id'].' - '.$value['itemName'].$fname.'</option>';
		}
		return $list;
	}

	function GetDTItems() {
		$rep=Array();
		$list = '';
		$wot = array('Truck','MechanicMapResource','MunicipalCenter','Sidewalk','Neighborhood','Airplane','CraftingCenter','HolidayTown','Ship','HarvestableShip','Decoration','Residence','Business','Municipal','Landmark','Wonder','Storage','Mall','Bridge','Hotel','PowerStation','ConstructionSite','Wilderness');
		$res=$this->ld->GetSelect("select * from dt_objects where className IN ('".implode("', '", $wot)."') order by className");
		if (!$res) { return "<option>DB ERROR</option>"; }
		$is=1;
		foreach ($res as $val) {
			$v=(string)$val[1];
			$obj=unserialize(base64_decode($v));
			$rep["obj"][$i] = $obj;
			$i++;
		}
		foreach ($rep["obj"] as $value) {
			$fname = ' - '.$this->flN((string)$value['itemName']);
			$list .= '<option value="'.$value['id'].'">'.$value['className'].' - '.$value['id'].' - '.$value['itemName'].$fname.'</option>';
		}
		return $list;
	}

	function GetForm() {
		echo '	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
				<html xmlns="http://www.w3.org/1999/xhtml" lang="ru">
				<head>
					<title>Send helper</title>
					<script src="..\..\codebase-php\jquery-1.4.2.min.js"></script>
					<script src="..\..\codebase-php\jquery.json-2.2.min.js"></script>
					<style>
						body{ background-color: rgb(255, 249, 200); font-size: 10pt; }
						.zag{ font-size: 15pt;  font-weight: bold; }
						#maint{ font-size: 10pt; width: 600px; padding: 0px; }
					</style>
					<script>
						//======================================================================
						$(document).ready(function(){
							window.settings=eval(' . json_encode($this->ld->GetPlSettings('Sendplugin')) . ');
							//==============================================================
							if ((window.settings!==null) && (window.settings!==undefined)){
								$("#itemid").val(window.settings.itemid);
								$("#dt_itemid").val(window.settings.dt_itemid);
								$(":checkbox").each(function(){
									if(window.settings[$(this).attr("id")]==true){
										$(this).attr("checked", true);
									}else{
										$(this).attr("checked", false);
									}
								});
							}
                            //==============================================================
							$("#btn_save").click(function(){
								var req=new Object();
								req.itemList=new Array();
								$("#itemid option:selected").each(function(){
									req.itemList.push($(this).val());
								});
								
								req.dt_itemList=new Array();
								$("#dt_itemid option:selected").each(function(){
									req.dt_itemList.push($(this).val());
								});
								
								$(":checkbox").each(function(){
									var par=$(this).attr("id");
                                    req[par]=$(this).attr("checked");
								});

								data=$.toJSON(req);
								var l=window.location.toString();
								var indx=l.indexOf(\'?\');
								var nurl=l.slice(0, indx)+"?action=save&tmp="+Math.random();
								$.post(nurl, data);
								//alert(req.itemList);	
								return false;
							});
						});
						//======================================================================
					</script>
				</head>
				<body>
					<form >
						<div class="zag" height="35">SendToInventory by vincenzo - based on Sell items plugin by 12Christiaan<br>Updated for Downtown by Little Todd<hr></div>
						<div style="margin-left: 20px">
							Version 0.03 - 7 sep 2011<br>
							<br>
							This plugin allows you to send items from your city to your inventory.<br>
							<br>';
							$list = $this->GetItems();
							$dt_list = $this->GetDTItems();
		echo '				Select the item from the list below and check the checkbox below to run the plugin next cycle, do not forget to save settings.<br>
							Only Decoration, Municipal, Residence and Business are listed.<br>
							<br>
							Use shift / ctrl for multi select.<br>
							<br>
							<table id="maint" border="0">
								<tr>
									<td colspan="2"><b>Main World</b><br>classname / item id / item name / real name <br>
										<select multiple="multiple" size="15" id="itemid" name="itemid">
											<option>Do not send item</option>';
		echo								$list;							 
		echo '							</select>
									</td>
								</tr>
							</table><br>
							<br>
							<table id="maint" border="0">
								<tr>
									<td colspan="2"><b>DownTown</b><br>classname / item id / item name / real name <br>
										<select multiple="multiple" size="15" id="dt_itemid" name="dt_itemid">
											<option>Do not send item</option>';
		echo								$dt_list;							 
		echo '							</select>
									</td>
								</tr>
							</table><br>
							<br>
							<input id="Send" type="checkbox" value="0" size="4"> Start sending items on next cycle.<br>
						</div>
						<div width="100%" align="center">
							<br>
							<button id="btn_save" style="color:white;background-color:#13a89e;border-width:1px;border-style:solid; ">&nbsp; Save settings&nbsp;</button>
						</div>
					</form>
				</body>
				</html>';
	}
}
class xmlsLang {
	var $langXML;
	function xmlsLang() {
		$langdir = substr($_SERVER["PHP_SELF"],0,strpos($_SERVER["PHP_SELF"],"/")).'tmp_dir\en_US.xml';
		$langs = simplexml_load_file($langdir);
		$arFname = array();
		foreach ($langs->pkg as $pkg) {
			if ($pkg['name'] == 'Items') {
				foreach ($pkg->string as $fname) {
					$fkey = substr((string)$fname['key'], 0, strpos((string)$fname['key'], '_friendlyName'));
					if (!empty($fkey)) {
						$arFname[$fkey] = (string)$fname->original;
					}
				}
			}
		}
		$this->langXML = $arFname;
	}
}

?>